<?php
namespace hangpu8\hpcms\Utils\Builder;
class ListBuilder
{
    // 数据
    private $data;
    public function __construct()
    {
        $this->data = [
            'topButtonList'         => [],
            'rightButtonList'       => [],
            'columns'               => [],
            'dataList'              => [],
            'paginate'              => [
                'total' => 0,
                'limit' => 0,
                'page' => 1
            ],
        ];
    }

    /**
     * 添加表格列
     *
     * @param string $field
     * @param string $title
     * @param array $extend
     * @return $this
     */
    public function addColumn(string $field,string $title,array $extend = [])
    {
        $column = [
            'field'             => $field,
            'title'             => $title,
            'extra'             => [
                'type'          => '',
                'width'         => '',
                'minWidth'      => '',
                'show'          => true,
                'loading'       => false,
                'options'       => [],
            ]
        ];
        $column['extra'] = array_merge($column['extra'], $extend);
        $this->data['columns'][] = $column;
        return $this;
    }

    /**
     * 设置列表数据
     *
     * @param array $dataList
     * @return $this
     */
    public function setData(array $dataList)
    {
        $this->data['dataList'] = $dataList;
        return $this;
    }

    /**
     * 设置数据分页
     *
     * @param integer $total
     * @param integer $limit
     * @param integer $page
     * @return $this
     */
    public function setDataPage(int $total, int $limit = 10, int $page = 1)
    {
        $this->data['paginate'] = [
            'total' => $total,
            'limit' => $limit,
            'page' => $page
        ];
        return $this;
    }

    /**
     * 添加顶部按钮
     *
     * @param [type] $name
     * @param [type] $title
     * @param array $pageData
     * @param array $style
     * @return $this
     */
    public function addTopButton($name, $title, $pageData = [], $style = [])
    {
        $btn['field'] = $name;
        $btn['title'] = $title;
        $btn['pageData'] = [
            'show'              => false,
            'title'             => '操作窗口',
            'pageType'          => 'modal', // 支持modal和page
            'modalType'         => 'form',
            'method'            => 'GET',
            'apiPrefix'         => [],
            'modalClosable'     => false,
            'width'             => '1000px',
            'height'            => '60vh'
        ];
        $btn['pageData'] = array_merge($btn['pageData'], $pageData);
        $btn['style'] = $style;
        $this->data['topButtonList'][] = $btn;
        return $this;
    }

    /**
     * 添加右侧菜单
     *
     * @param string $field
     * @param string $title
     * @param array $pageData
     * @param array $style
     * @return $this
     */
    public function addRightButton(string $field, string $title, array $pageData = [], array $style = []) {
        $btn = $this->getRightButton($field, $title, $pageData, $style);
        $this->data['rightButtonList'][] = $btn;
        return $this;
    }

    /**
     * 构造右侧菜单数据
     *
     * @param [type] $name
     * @param [type] $title
     * @param array $pageData
     * @param array $style
     * @return $this
     */
    private function getRightButton($name, $title, $pageData = [], $style = []) {
        $btn = [];
        $btn['field'] = $name;
        $btn['title'] = $title;
        $btn['pageData'] = [
            'show'          => false,
            'method'        => '',
            'pageType'      => 'modal', // 支持modal和page
            'modalType'     => 'form',
            'apiPrefix'     => [],
            'modalClosable' => false,
            'width'         => '1000px',
            'height'        => '60vh',
        ];
        $btn['pageData'] = array_merge($btn['pageData'], $pageData);
        $btn['style'] = $style;
        return $btn;
    }

    /**
     * 构造数据
     *
     * @return array
     */
    public function create() : array
    {
        // p($this->data);exit;
        return $this->data;
    }
}